/**
XenSim simulates xenbal.
Copyright (C) 2006 Travis Vachon

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/
package xensim;


public class BalanceEventSI extends Event {
	private Node node;
	private SimGlobals globals;
	private Monitor monitor;
	
	public BalanceEventSI(double executionTime, SimGlobals globals, Monitor monitor,Node node){
		super(executionTime);
		this.node = node;
		this.globals = globals;
		this.monitor = monitor;
	}
	@Override
	public void execute() throws ConfigurationError{
		
		// check for migration possiblities on each node
		
		
		double load;
		
		load = monitor.getTotalLoad(node);
		//System.out.println(load);
		if (node.numberOfDomains() > 2) {
			if (load > SimGlobals.FULLY_LOADED_THRESHOLD) {
				//System.out.println(load);
				// choose a domain
				double largest_load = 0;
				Domain chosenDomain = null;
				for (Domain domain : node.domains) {
					if (monitor.getDomainLoad(domain) > largest_load
							&& domain.name != "dom0"
							&& !domain.migrating
							&& (domain.timeStartedOnNode < globals.currentTime - 
									(SimGlobals.MIN_NODE_TIME*SimGlobals.TIME_CONSTANT))) {
						//System.out.println("DomLoad: "+ monitor.getDomainLoad(domain));
						
						chosenDomain = domain;
					}
				}
				
				// if a domain is chosen, choose a destination
				Node chosenNode = null;
				if (chosenDomain != null) {
					
					double smallest_load = SimGlobals.PEER_LOAD_THRESHOLD;
					
					for (Node node : globals.nodes) {
						load = monitor.getTotalLoad(node);
						//System.out.println(node.name + " " + node.numberOfDomains());
						//System.out.println("Load = " + load);
						if (!(this.node == node)
								&& !this.node.migrating
								&& !node.migrating
								&& load < SimGlobals.PEER_LOAD_THRESHOLD
								&& load < smallest_load) {
							chosenNode = node;
							smallest_load = load;
							
						}
					}
					if (chosenNode == null){
						for (Node node : globals.nodes) {
							if(!(this.node == node)
									&& node.domains.size() < this.node.domains.size() - 1){
								chosenNode = node;
							}
						}
					}
				}
				
				
				
				if (chosenDomain != null && chosenNode != null) {
					globals.eventQueue.add(new MigrationEvent(
							executionTime, this.node, chosenDomain,
							chosenNode));
					System.out.println("migrating "+chosenDomain.name + " to " + chosenNode.name);
					
				}
				
			}
		}
		
		
		globals.eventQueue.add(new BalanceEventSI(executionTime + SimGlobals.BALANCER_INTERVAL, this.globals,monitor,this.node));
		
		
	}
	
}
